﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Axis.Dao.Research;
using Axis.BusinessObject.Research;
using Axis.Web.Administrator.Global;
using System.Web.Script.Serialization;
using Axis.BusinessObject.Core;
using System.Text;
using System.Web.SessionState;

namespace Axis.Web.Administrator.Views.Project.Service
{
    /// <summary>
    /// Summary description for QuestionService
    /// </summary>
    public class QuestionService : IHttpHandler, IReadOnlySessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            string action = context.Request["Action"];
            string output = string.Empty;
            switch (action)
            {
                case "INSERT":
                case "UPDATE":
                case "DELETE":
                    output = UpdateQuestion(context);
                    break;
                default:
                    output = GetList(context);
                    break;
            }
            context.Response.Write(output);
        }
        protected string UpdateQuestion(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            CQuestionUI questionUI = new CQuestionUI();
            /* Get QueryString to xml */
            CCore cCore = new CCore();
            string inputValue = questionUI.GetXmlRequest(context);
            CResearchDao core = new CResearchDao();
            CApplicationMessage message = core.ActionQuestionaire(inputValue);
            string output = new JavaScriptSerializer().Serialize(message);
            return output;
        }
        protected string GetList(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;
            string _search = request["_search"];
            string numberOfRows = request["rows"];
            string pageIndex = request["page"];
            string sortColumnName = request["sidx"];
            string sortOrderBy = request["sord"];
            CCore cCore = new CCore();
            string inputValue = cCore.GetInputValue(context);
            CResearchDao core = new CResearchDao();
            IList<CQuestionaire> list = core.GetQuestionaire(inputValue);
            int totalRecords = core.LastOutputValue.TotalRows;
            int totalPages = core.LastOutputValue.TotalPages;
            string output = BuildJQGridResults(list, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), totalPages);
            return output;
        }
        protected string BuildJQGridResults(IList<CQuestionaire> projects, int numberOfRows, int pageIndex, int totalPages)
        {
            if (projects != null && projects.Count > 0)
            {
                CGrid.JQGridResults result = new CGrid.JQGridResults();
                List<CGrid.JQGridRow> rows = new List<CGrid.JQGridRow>();
                foreach (CQuestionaire project in projects)
                {
                    CGrid.JQGridRow row = new CGrid.JQGridRow();
                    row.id = project.ID;
                    row.cell = new string[21];
                    row.cell[0] = project.ID.ToString();
                    row.cell[2] = project.Code.ToString();
                    row.cell[1] = project.ZOrder.ToString();
                    row.cell[3] = project.QuestionText;
                    row.cell[4] = project.TypeName;
                    row.cell[5] = project.Type.ToString();
                    row.cell[6] = project.MaxResponseCount.ToString();
                    row.cell[7] = project.ProjectName;
                    row.cell[8] = project.ProjectID.ToString();
                    row.cell[9] = project.Status.ToString();
                    row.cell[10] = project.Status.ToString();
                    //
                    row.cell[11] = "";
                    row.cell[12] = project.ScaleFrom.ToString();
                    row.cell[13] = project.ScaleFromText.ToString();
                    row.cell[14] = project.ScaleTo.ToString();
                    row.cell[15] = project.ScaleToText.ToString();
                    row.cell[16] = project.DependentID.ToString();
                    row.cell[17] = "0";
                    if (project.DisplayRandomResponse == true)
                        row.cell[17] = "1";
                    row.cell[18] = project.DependentType.ToString();
                    row.cell[19] = project.ContentType.ToString();
                    row.cell[20] = "";
                    if (project.MediaUrl != null)
                        row.cell[20] = project.MediaUrl.ToString();
                    rows.Add(row);
                }
                result.rows = rows.ToArray();
                result.page = pageIndex;
                result.total = totalPages;
                result.records = numberOfRows;
                return new JavaScriptSerializer().Serialize(result);
            }
            else
            {
                return string.Empty;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}